import { createFileFromBase64 } from "@/utils";
import { useEffect, useState } from "react";

const useBase64ToFileList = (
  base64String: string,
  fileName: string,
  fileType: string
) => {
  const [fileList, setFileList] = useState<any[]>([]);

  useEffect(() => {
    // 确保传入的参数有效
    if (base64String && fileName && fileType) {
      const file = createFileFromBase64(base64String, fileName, fileType);
      console.log(file, "file1");

      const objectURL = URL.createObjectURL(file);
      setFileList([
        {
          uid: "-1",
          name: file.name,
          status: "done",
          url: objectURL,
          originFileObj: file,
        },
      ]);

      // 清理函数，释放 URL 对象
      return () => {
        URL.revokeObjectURL(objectURL);
      };
    } else {
      setFileList([]); // 如果输入无效，清空 fileList
    }
  }, [base64String, fileName, fileType]);

  return fileList;
};

export default useBase64ToFileList;
